import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_types.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/models/user/xindong_user.dart';
import 'package:xindong/routes/tab_home/xindong_search_result_yemian.dart';
import 'package:xindong/widgets/common/xindong_app_bar.dart';
import 'package:xindong/widgets/common/xindong_app_bar_text_button.dart';
import 'package:xindong/widgets/common/xindong_search_text_field.dart';
import 'package:xindong/widgets/common/xindong_text_field.dart';
import 'package:xindong/widgets/dialog/xindong_cities_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_options_sheet.dart';
import 'package:xindong/widgets/dialog/xindong_selector2_sheet.dart';
import 'package:xindong/widgets/xindong_common_select_item.dart';

import 'controller/xindong_search_vm.dart';

class XinDongSearchYeMian extends StatefulWidget {
  const XinDongSearchYeMian({super.key});

  @override
  State<XinDongSearchYeMian> createState() => _XinDongSearchYeMianState();
}

class _XinDongSearchYeMianState extends State<XinDongSearchYeMian> {
  final TextEditingController textEditingController = TextEditingController();
  final _controller = Get.put(XinDongHomeSearchVM());

  onSearch() {
    final text = textEditingController.text.trim();
    if (text.isEmpty || int.tryParse(text) == null) {
      _controller.keyword = text;
      _controller.dataList = null;
      XinDongPageSingleton.instance.pushYeMian(const XinDongSearchResultYeMian());
      return;
    }
    final user = XinDongUser()..user_id = text;
    XinDongPageSingleton.instance.jumpToSpace(user);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).requestFocus(FocusNode()), // 触摸收起键盘
      child: Scaffold(
        appBar: XinDongAppBar(
          "",
          titleSpacing: 0,
          titleView: _searchBarWidget(),
          actions: [
            XinDongAppBarTextButton(
              name: "搜索",
              color: DZColors.titleColor,
              onTap: onSearch,
            ),
          ],
        ),
        body: GetBuilder<XinDongHomeSearchVM>(builder: (viewClass) {
          return ListView(
            children: [
              XinDongCommonSelectItem(
                title: "性别",
                value: viewClass.sex,
                onTap: () async {
                  const dataList = [Gender.male, Gender.female];
                  XinDongOptionsSheet.show(
                    dataList: dataList,
                    onSelect: (index) {
                      if (index >= 0 && index < dataList.length) {
                        viewClass.sex = dataList[index];
                        viewClass.update();
                      }
                    },
                  );
                },
              ),
              XinDongCommonSelectItem(
                title: "认证动态",
                value: viewClass.auth,
                onTap: () async {
                  const dataList = ["全部", "真人认证"];
                  XinDongOptionsSheet.show(
                    dataList: dataList,
                    onSelect: (index) {
                      if (index >= 0 && index < dataList.length) {
                        viewClass.auth = dataList[index];
                        viewClass.update();
                      }
                    },
                  );
                },
              ),
              XinDongCommonSelectItem(
                title: "所在地",
                value: viewClass.city,
                onTap: () async {
                  XinDongCitiesSheet.showCity(onDone: (text) {
                    viewClass.city = text;
                    viewClass.update();
                  });
                },
              ),
            ],
          );
        }),
      ),
    );
  }

  Widget _searchBarWidget() {
    return Container(
      height: 32,
      width: double.infinity,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        color: DZColors.backgroundColor,
        borderRadius: BorderRadius.all(Radius.circular(32)),
      ),
      child: XinDongSearchTextField(
        digitsOnly: false,
        placeholder: "请输入关键词或ID搜索",
        controller: textEditingController,
        onSubmitted: (text) => onSearch(),
      ),
    );
  }
}
